package com.example.rbacmanage.controller.system;

import com.example.rbacmanage.annotation.LogAnnotation;
import com.example.rbacmanage.common.Result;
import com.example.rbacmanage.entity.system.Role;
import com.example.rbacmanage.service.system.RoleService;
import com.example.rbacmanage.vo.RolePageQueryReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('system:role:list')")
    public Result getRolePage(RolePageQueryReq req) {
        Role role = new Role();
        role.setPage(req.getPage());
        role.setLimit(req.getLimit());
        role.setRoleName(req.getRoleName());
        role.setRoleMark(req.getRoleMark());
        role.setStatus(req.getStatus());
        return Result.success(roleService.getPage(role));
    }

    @GetMapping("/list")
    public Result getRoleList(Role role) {
        return Result.success(roleService.getAll(role));
    }

    @PostMapping("/add")
    @LogAnnotation(module = "角色模块",operation = "新增角色")
    @PreAuthorize("hasAnyAuthority('system:role:add')")
    public Result save(@RequestBody Role role) {
        roleService.save(role);
        return Result.success();
    }

    @PutMapping("/update")
    @LogAnnotation(module = "角色模块",operation = "修改角色")
    @PreAuthorize("hasAnyAuthority('system:role:edit')")
    public Result update(@RequestBody Role role) {
        roleService.update(role);
        return Result.success();
    }

    @PostMapping("/assignPermission")
    @LogAnnotation(module = "角色模块",operation = "分配权限")
    @PreAuthorize("hasAnyAuthority('system:role:assignPermission')")
    public Result assignPermission(@RequestBody Role role) {
        roleService.assignPermission(role);
        return Result.success();
    }


    /**
     * 给角色分配用户
     */
    @PreAuthorize("hasAnyAuthority('system:role:assignUsers')")
    @LogAnnotation(module = "角色模块",operation = "分配用户")
    @PostMapping(value = "/assignUsers")
    public Result assignUsers(@RequestBody Role role) {
        roleService.assignUsers(role);
        return Result.success();
    }

    /**
     * 给角色取消分配用户
     */
    @PreAuthorize("hasAnyAuthority('system:role:unAssignUsers')")
    @LogAnnotation(module = "角色模块",operation = "取消分配用户")
    @DeleteMapping(value = "/unAssignUsers")
    public Result unAssignUsers(@RequestBody Long[] ids) {
        roleService.unAssignUsers(ids);
        return Result.success();
    }


    @DeleteMapping("/remove")
    @LogAnnotation(module = "角色模块", operation = "删除角色")
    @PreAuthorize("hasAnyAuthority('system:role:delete')")
    public Result remove(@RequestBody Long[] ids)  {
        roleService.remove(ids);
        return Result.success();
    }


    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        return Result.success(roleService.getById(id));
    }
}
